#!/bin/sh
#
# Run a simple backup  using the compressed option
#   then backup four times, each with incremental then finally restore.
#   It should require at least 4 different bsrs.
#
TestName="subscription-test"
JobName=BackupClient1
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
/bin/cp -f  ${rconfigs}/${TestName}/susan-dir.conf.${TestName} ${conf}/susan-dir.conf
change_jobname $JobName

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
messages
label storage=File volume=TestVolume001
status subscriptions
run job=$JobName yes
wait
messages
quit
END_OF_DATA
run_susan

scripts/check_for_zombie_jobs storage=file client=client

echo "Backup 1 done"

stop_susan

# check if warning after backup was issued
grep "JobId.*Warning: Subscriptions exceeded:" ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Check for subscription: Warning after Backup is issued."
else
   estat=1
fi
#

# check status subscriptions show a warning
grep "^Warning! No available subscriptions.*" ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Check for subscription: interactive check works"
else
   estat=2
fi
#


# update subscriptions to 5
cp ${conf}/susan-dir.conf ${tmp}/susan-dir.conf.tmp
sed 's#Subscriptions = 1#Subscriptions = 5#g' < ${tmp}/susan-dir.conf.tmp > ${conf}/susan-dir.conf
change_files

#
# run second backup
#

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
messages
status subscriptions
run job=$JobName yes
wait
messages
quit
END_OF_DATA
run_susan

scripts/check_for_zombie_jobs storage=file

echo "Backup 2 done"

# check if warning after backup was issued
grep "JobId.*Warning: Subscriptions exceeded:" ${tmp}/log2.out 2>&1 >/dev/null
if test $? -ne 0; then
   print_debug "Check for subscription: Warning after Backup is not issued. Correct."
else
   estat=3
fi
#
#

# check "status subscriptions" show a warning
grep "^Ok: available subscriptions.*" ${tmp}/log2.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Check for subscription: interactive check still gives  Ok!"
else
   estat=4
fi
#


stop_susan

end_test
